<?php
declare (strict_types = 1);

namespace app\middleware;

use think\exception\HttpResponseException;
use think\facade\Log;

class CommonLog
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        if(env("app_debug") === true) {
            $host = $request->server("HTTP_HOST");
            $uri = $request->server("REQUEST_URI");
            $ua = $request->server("HTTP_USER_AGENT");
            $method = $request->server("REQUEST_METHOD");
//            $param = $request->request();
            $param = $request->param();
            //放入数组
            $content = [
                "host"=>$host,
                "uri"=>$uri,
                "ua"=>$ua,
                "method"=>$method,
                "param"=>$param,
            ];
//            pp($content);die();

            $contentStr = var_export($content,true);
            $ip = request()->ip();

            Log::record(
                $ip . " POST " . $uri . "\r\n" . $contentStr . "\r\n","log", [],false
            );
//            throw new HttpResponseException(ajax('中间件返回数据!',1));
        }

        return $next($request);
    }
}
